<?php
declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Events\Saving;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Database\Model\SoftDeletes;
use League\Flysystem\Filesystem;
use Hyperf\Di\Annotation\Inject;

/**
 * @property $id
 * @property $name
 * @property $created_at
 * @property $updated_at
 */
class UserMaterial extends Model
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected? string $table = 'user_materials';

     /**
     * @var Filesystem
     */
    #[Inject]
    private Filesystem $filesystem;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $fillable = ['id', 'ext', 'infringement', 'shape', 'size', 'name', 'user_id', 'category_id', 'tags', 'url', 'created_at', 'updated_at'];
    protected array $hidden = ['deleted_at'];

    protected array $appends = [
        'full_url'
    ];
    protected array $casts = [
        'tags' => 'array',
    ];


    public function getFullUrlAttribute(){
        if(!$this->url)return '';
        return $this->filesystem->publicUrl($this->url);
    }

    public function category(){
        return $this->hasOne(UserCategory::class,'id','category_id');
    }


}